package lacus.wd_cinema.controller.api;

import jakarta.servlet.http.HttpServletRequest;
import lacus.wd_cinema.dto.tickets.TicketBuyRequest;
import lacus.wd_cinema.dto.tickets.TicketBuyResponse;
import lacus.wd_cinema.entity.User;
import lacus.wd_cinema.service.AuthService;
import lacus.wd_cinema.service.TicketService;
import org.springframework.web.bind.annotation.*;

/**
 * 电影票操作接口控制类
 * @author lacus
 * @version 0.6
 * @since 0.6
 */
@RestController
@RequestMapping("/api/tickets")
public class TicketController {
    private final TicketService ticketService;
    private final AuthService authService;

    public TicketController(TicketService ticketService, AuthService authService) {
        this.ticketService = ticketService;
        this.authService = authService;
    }

    @PostMapping
    public TicketBuyResponse buyTicket(
            @RequestBody TicketBuyRequest ticketBuyRequest,
            HttpServletRequest httpServletRequest) {
        User purchaser = authService.getCurrentUser(httpServletRequest);
        return ticketService.buyTicket(ticketBuyRequest, purchaser);
    }

    @DeleteMapping("/{id}")
    public void refundTicket(
            @PathVariable String id,
            HttpServletRequest httpServletRequest) {
        User operator = authService.getCurrentUser(httpServletRequest);
        ticketService.refundTicket(id, operator);
    }


}
